﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using pdtransport_v2._0.Models;

namespace pdtransport_v2._0.Areas.back.Controllers
{
    public class kh_tk
    {
        public KhachHangThanhVien khachhang;
        public TaiKhoanKhachHangThanhVien taikhoan;
    }

    public class KhachhangController : Controller
    {
        //
        // GET: /back/Khachhang/
        pdtransportDataContext datacontext = new pdtransportDataContext();
        HelpController help = new HelpController();

        [Authorize(Roles = "admin")]
        public ActionResult DanhSach(string keyword)
        {
            int total_rows = 0;
            List<kh_tk> kh;
            if (keyword == null)
            {
                total_rows = (from _kh in datacontext.KhachHangThanhViens
                              join _tk in datacontext.TaiKhoanKhachHangThanhViens on _kh.IDKHTV equals _tk.KHTV
                              where _kh.TinhTrang == 1
                              select new kh_tk { khachhang = _kh, taikhoan = _tk }).Count();
            }
            else
            {
                total_rows = (from _kh in datacontext.KhachHangThanhViens
                              join _tk in datacontext.TaiKhoanKhachHangThanhViens on _kh.IDKHTV equals _tk.KHTV
                              where _kh.TinhTrang == 1 && (_kh.HoTen.Contains(keyword) == true || _tk.username.Contains(keyword) == true)
                              select new kh_tk { khachhang = _kh, taikhoan = _tk }).Count();
            }

            ViewBag.total_row = total_rows;
            string base_URL = "KhachHang/DanhSach"; // tên method của controller KhachHangController

            // phan trang
            string URL_segment;
            try
            {
                URL_segment = Request.Url.Segments[4];
            }
            catch (Exception)
            {
                URL_segment = null;
            }

            Libraries.Pagination pagination = new Libraries.Pagination(base_URL, URL_segment, total_rows);
            string pageLinks = pagination.GetPageLinks();
            int start = (pagination.CurPage - 1) * pagination.PerPage;
            int offset = pagination.PerPage;
            if (URL_segment != null)
                pageLinks = pageLinks.Replace(base_URL + "/", "");
            ViewBag.pageLinks = pageLinks;
            // phan trang

            if (keyword == null)
            {
                kh = (from _kh in datacontext.KhachHangThanhViens
                              join _tk in datacontext.TaiKhoanKhachHangThanhViens on _kh.IDKHTV equals _tk.KHTV
                              where _kh.TinhTrang == 1
                      select new kh_tk { khachhang = _kh, taikhoan = _tk }).Skip(start).Take(offset).ToList();
            }
            else
            {
                kh = (from _kh in datacontext.KhachHangThanhViens
                              join _tk in datacontext.TaiKhoanKhachHangThanhViens on _kh.IDKHTV equals _tk.KHTV
                              where _kh.TinhTrang == 1 && (_kh.HoTen.Contains(keyword) == true || _tk.username.Contains(keyword) == true)
                              select new kh_tk { khachhang = _kh, taikhoan = _tk }).Skip(start).Take(offset).ToList();
            }

            ViewBag.khachhang = kh;
            return View();
        }

        

        // phần này chuyển sang trang cho khách hàng đăng ký và sửa khách hàng thành viên
        [HttpGet]
        public ActionResult Them()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Them(string hoten, string email, string sodt, string chucdanh, string congty, string fax, string username, string password)
        {
            KhachHangThanhVien kh = new KhachHangThanhVien();
            TaiKhoanKhachHangThanhVien tk = new TaiKhoanKhachHangThanhVien();

            kh.HoTen = hoten;
            kh.Email = email;
            kh.SoDT = sodt;
            kh.ChucDanh = chucdanh;
            kh.CongTyCuaKhachHang = congty;
            kh.Fax = fax;
            kh.TinhTrang = 1;

            datacontext.KhachHangThanhViens.InsertOnSubmit(kh);
            datacontext.SubmitChanges();

            int idkh = datacontext.KhachHangThanhViens.OrderByDescending(_kh => _kh.IDKHTV).Take(1).Single().IDKHTV;

            tk.KHTV = idkh;
            tk.username = username;
            tk.password = help.Encrypt(password);
            tk.TinhTrang = 1;

            datacontext.TaiKhoanKhachHangThanhViens.InsertOnSubmit(tk);
            datacontext.SubmitChanges();

            ViewBag.Them = 1;

            return View();
        }

        [HttpGet]
        public ActionResult Sua(int idkh)
        {
            KhachHangThanhVien kh = (from _khtv in datacontext.KhachHangThanhViens
                                     where _khtv.IDKHTV == idkh
                                     select _khtv).Single();

            ViewBag.TaiKhoan = (from _tk in datacontext.TaiKhoanKhachHangThanhViens
                                where _tk.KHTV == idkh
                                select _tk).Single();
            return View(kh);
        }

        public ActionResult Sua(int idkh, string hoten, string email, string sodt, string chucdanh, string congty, string fax, string old_password, string new_password)
        {
            KhachHangThanhVien kh = (from _khtv in datacontext.KhachHangThanhViens
                                     where _khtv.IDKHTV == idkh
                                     select _khtv).Single();

            TaiKhoanKhachHangThanhVien tk = (from _tk in datacontext.TaiKhoanKhachHangThanhViens
                                             where _tk.KHTV == idkh
                                             select _tk).Single();

            string encrypt_pass = help.Encrypt(old_password);

            if (tk.password != encrypt_pass)
            {
                ViewBag.wrongpass = 1;
            }
            else
            {
                kh.HoTen = hoten;
                kh.Email = email;
                kh.SoDT = sodt;
                kh.ChucDanh = chucdanh;
                kh.CongTyCuaKhachHang = congty;
                kh.Fax = fax;

                tk.password = help.Encrypt(new_password);
            }

            datacontext.SubmitChanges();
            ViewBag.success = 1;
            return RedirectToAction("Sua", new { idkh = idkh });
        }
        // kết thúc phần dành cho khách hàng đăng ký và sửa khách hàng thành viên
        
        public ActionResult Xoa(int idkh)
        {
            KhachHangThanhVien khtv = (from _khtv in datacontext.KhachHangThanhViens
                                       where _khtv.IDKHTV == idkh
                                       select _khtv).Single();
            khtv.TinhTrang = 0;
            TaiKhoanKhachHangThanhVien tkkh = (from _tkkh in datacontext.TaiKhoanKhachHangThanhViens
                                               where _tkkh.KHTV == idkh
                                               select _tkkh).Single();
            tkkh.TinhTrang = 0;

            datacontext.SubmitChanges();

            ViewBag.Xoa = 1;
            return RedirectToAction("DanhSach");
        }
                

    }
}
